﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace chess
{

    public class csBoard
    {
        csField[,] _board = new csField[8, 8];

        public csBoard()
        {
            this.Initialize();
        }

        public void Initialize()
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    this._board[i, j] = new csField(enumFieldStatus.Empty, i, j);
                }
            }
        }

        public void placeFig(csFig fig)
        {
            _board[fig.Spalte, fig.Zeile].SetFig(fig);
        }

        public csField GetField(int spalte, int zeile)
        {
            return this._board[spalte, zeile];
        }

        public int EvaluateSituationFor(enumFigParty party)
        {
            List<csFig> figList = GetAllActive(party);

            int result = 0;
            foreach (csFig fig in figList)
            {
                result += fig.Value;
            }

            return result;
        }
        
        public List<csFig> GetAllActive(enumFigParty party)
        {
            List<csFig> figResult = new List<csFig>();

            for (int j = 0; j < 8; j++)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (this._board[i, j].Status != enumFieldStatus.Empty)
                    {
                        csFig tmpFig = this._board[i, j].GetFig;
                        if ((tmpFig.Status == enumFigStatus.active)&&(tmpFig.Party == party))
                        {
                            figResult.Add(tmpFig);
                        }
                    }
                }
            }
            return figResult;
        }

        public void Move(csFig fig, int spalte, int zeile)
        {
            this._board[fig.Spalte, fig.Zeile].ClearField();
            this._board[spalte, zeile].ClearField();
            fig.SetField(spalte, zeile);
            this._board[spalte, zeile].SetFig(fig);
            //fig.ErsterZugGemacht = true;
        }
    }
}
